unit clsDetalhe_Demonstrativo;

interface

uses Dm,dbtables,biblio;

type
  TDetalhe_Demonstrativo = class(TObject)
  private
    Dem_codigo :string;
    Det_item:string;
  public
    Det_Descricao  :string;
    Det_Valor      :double;
    Det_Tipo       :string;
    function  Gravar(P_Dem_codigo,P_Det_item:String):Byte;
    function  Recuperar(P_Dem_codigo,P_Det_item:String):Byte;
    function  Excluir(P_Dem_codigo,P_Det_item:String):Byte;
    Function  Excluir_Dem(P_Dem_codigo:string):byte;
    procedure Consultar(var p_q:TQuery; P_Dem_codigo:String);
    function  Atualizar(P_Dem_codigo,P_Det_item:String):Byte;
  end;

implementation

uses DB;

{ TDetalhe_Demonstrativo }

function TDetalhe_Demonstrativo.Atualizar(P_Dem_codigo,
  P_Det_item: String): Byte;
var tq:TQuery;
begin
  tq:=Tquery.Create(DmInt);
  tq.databasename:=dmint.Db.DatabaseName;
  tq.active:=False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('UPDATE Detalhe SET Det_Valor=:K_Det_Valor WHERE Dem_Codigo=:k_Dem_Codigo and Det_item=:K_Det_item');
  tq.parambyname('k_Dem_Codigo').asstring   :=strzero(p_dem_Codigo,2);
  tq.parambyname('K_Det_item').asstring     :=strzero(P_Det_item,2);
  tq.parambyname('K_Det_valor').AsFloat     :=det_valor;
  tq.close;
  tq.execsql;
  result:=1;
  tq.Free;
end;

procedure TDetalhe_Demonstrativo.Consultar(var p_q: TQuery;
  P_Dem_codigo: String);
begin
  p_q.sql.clear;
  p_q.params.clear;
  p_q.SQL.add('Select * from Detalhe where Dem_codigo =:K_Dem_Codigo order by det_item');
  p_q.ParamByName('K_Dem_codigo').AsString := strzero(P_Dem_codigo,2);
end;

function TDetalhe_Demonstrativo.Excluir(P_Dem_codigo,
  P_Det_item: String): Byte;
var tq:TQuery;
begin
  tq:=Tquery.Create(dmint);
  tq.databasename:=dmint.db.databasename;
  tq.active:=False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Delete FROM Detalhe where Dem_codigo=:k_Dem_Codigo and Det_item=:K_Det_item');
  tq.parambyname('k_Dem_Codigo').asstring:=strzero(P_Dem_codigo,2);
  tq.parambyname('k_Det_item').asstring:=strzero(P_Det_item,2);
  tq.execSQL;
  Result:=1;
  tq.close;
  tq.Free;
end;

function TDetalhe_Demonstrativo.Excluir_Dem(P_Dem_codigo: string): byte;
var tq:TQuery;
begin
  tq:=Tquery.Create(dmint);
  tq.databasename:=dmint.db.databasename;
  tq.active:=False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Delete FROM Detalhe where Dem_codigo=:k_Dem_Codigo');
  tq.parambyname('k_Dem_Codigo').asstring:=strzero(P_Dem_codigo,2);
  tq.execSQL;
  Result:=1;
  tq.close;
  tq.Free;
end;

function TDetalhe_Demonstrativo.Gravar(P_Dem_codigo,
  P_Det_item: String): Byte;
var tq:TQuery;
begin
  tq:=Tquery.Create(DmInt);
  tq.databasename:=dmint.Db.DatabaseName;
  tq.active:=False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM Detalhe where Dem_codigo=:k_Dem_Codigo and Det_item=:K_Det_item');
  tq.parambyname('k_Dem_Codigo').asstring:=strzero(P_Dem_codigo,2);
  tq.parambyname('k_Det_item').asstring:=strzero(P_Det_item,2);
  tq.open;
  if tq.eof then begin
    tq.sql.clear; tq.params.clear;
    tq.sql.add('INSERT INTO Detalhe VALUES(:k_Dem_Codigo,:K_Det_item,:K_Det_Descricao,:K_Det_Valor,:k_Det_Tipo)');
  end else begin
    tq.sql.clear; tq.params.clear;
    tq.sql.add('UPDATE Detalhe SET Det_Descricao=:k_Det_Descricao, Det_Valor=:K_Det_Valor,Det_Tipo=:k_Det_Tipo WHERE Dem_Codigo=:k_Dem_Codigo and Det_item=:K_Det_item');
  end;
  tq.parambyname('k_Dem_Codigo').asstring   :=strzero(p_dem_Codigo,2);
  tq.parambyname('K_Det_item').asstring     :=strzero(P_Det_item,2);
  tq.parambyname('K_Det_Descricao').asstring:=det_descricao;
  tq.parambyname('K_Det_valor').AsFloat     :=det_valor;
  tq.parambyname('k_Det_Tipo').AsString     := Det_Tipo;
  tq.close;
  tq.execsql;
  result:=1;
  tq.Free;
end;

function TDetalhe_Demonstrativo.Recuperar(P_Dem_codigo,
  P_Det_item: String): Byte;
var tq:TQuery;
begin
  tq:=Tquery.Create(dmint);
  tq.databasename:=DmInt.Db.DatabaseName;
  tq.active:=False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM Detalhe where Dem_codigo=:k_Dem_Codigo and Det_item=:K_Det_item');
  tq.parambyname('k_Dem_Codigo').asstring:=strzero(P_Dem_codigo,2);
  tq.parambyname('k_Det_item').asstring:=strzero(P_Det_item,2);
  tq.open;
  dem_Codigo       :=p_dem_Codigo;
  Det_item      :=P_Det_item;
  if tq.eof then begin
    Det_Descricao:='';
    Det_Valor    :=0;
    Det_Tipo     := '';
    Result:=0;
  end else begin
    Det_Descricao:=tq.fieldbyname('Det_Descricao').asstring;
    Det_Valor    :=tq.fieldbyname('Det_Valor').AsFloat;
    Det_Tipo     :=tq.fieldbyname('Det_Tipo').AsString;
    Result:=1;
  end;
  tq.Close;
  tq.Free;
end;

end.
